package com.cenxi.controller;

import com.cenxi.common.constants.Constants;
import com.cenxi.common.dto.req.MenuReqDTO;
import com.cenxi.common.dto.resp.MenuRespDTO;
import com.cenxi.common.util.req.PageReq;
import com.cenxi.common.util.req.Req;
import com.cenxi.common.util.resp.PageResp;
import com.cenxi.common.util.resp.Resp;
import com.cenxi.manage.ManageMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @Author cenxi
 * @Date 2023/8/19 14:48
 * @Version 1.0
 */

@RestController
@RequestMapping("/menu")
@Api(tags = {"菜单管理"})
public class MenuController {

    @Autowired
    private ManageMenuService manageMenuService;

    @PostMapping("/page")
    @ApiOperation(value = "获取平台菜单权限分页列表")
    public PageResp<MenuRespDTO> page(@RequestBody PageReq<MenuReqDTO> req) {
        return manageMenuService.page(req);
    }

    @PostMapping("/create")
    @ApiOperation(value = "创建菜单权限")
    public Resp<Boolean> create(@RequestBody Req<MenuReqDTO> req) {
        return manageMenuService.create(req);
    }

    @PostMapping("/findById")
    @ApiOperation(value = "根据id查询平台菜单权限信息")
    public Resp<MenuRespDTO> findById(@RequestBody Req<String> req) {
        if (Objects.isNull(req.getData())) {
            return Resp.error("参数错误");
        }
        return manageMenuService.findById(req.getData());
    }

    @PostMapping("/edit")
    @ApiOperation(value = "修改菜单权限")
    public Resp<Boolean> edit(@RequestBody Req<MenuReqDTO> req) {
        return manageMenuService.edit(req);
    }

    @PostMapping("/enable")
    @ApiOperation(value = "启用菜单权限")
    public Resp<Boolean> enable(@RequestBody Req<String> req) {
        if (Objects.isNull(req.getData())) {
            return Resp.error("参数错误");
        }
        return manageMenuService.enable(req.getData(), Constants.ENABLE);
    }

    @PostMapping("/disable")
    @ApiOperation(value = "禁用菜单权限")
    public Resp<Boolean> disable(@RequestBody Req<String> req) {
        if (Objects.isNull(req.getData())) {
            return Resp.error("参数错误");
        }
        return manageMenuService.disable(req.getData(), Constants.DISABLE);
    }

    @PostMapping("/deleteById")
    @ApiOperation(value = "根据id删除平台菜单权限信息")
    public Resp<Boolean> deleteById(@RequestBody Req<String> req) {
        if (Objects.isNull(req.getData())) {
            return Resp.error("参数错误");
        }
        return manageMenuService.deleteById(req.getData());
    }

    @PostMapping("/selectTree")
    @ApiOperation(value = "获取菜单权限选择树")
    public Resp<List<MenuRespDTO>> selectTree() {
        return manageMenuService.selectTree();
    }

    @PostMapping("/router")
    @ApiOperation(value = "获取菜单权限路由列表")
    public Resp<List<MenuRespDTO>> getRouters() {
        return manageMenuService.getRouters();
    }
}
